package cn.stephen.study.demoproject.controller;

import cn.stephen.study.demoproject.dao.dbone.PatrolMapper;
import cn.stephen.study.demoproject.dao.dbone.PatrolValueMapper;
import cn.stephen.study.demoproject.entity.Patrol;
import cn.stephen.study.demoproject.entity.PatrolValue;
import cn.stephen.study.demoproject.service.PatrolService;
import cn.stephen.study.demoproject.util.PageUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/patrol")
public class PatrolController {
    @Autowired
    private PatrolService patrolService;
    @Autowired
    private PatrolValueMapper patrolValueMapper;
    @Autowired
    private PatrolMapper patrolMapper;


    /**
     * 根据条件查询巡检项
     * page:第几页
     * limit：pageSize
     *
     * @return
     */
    @RequestMapping("findByValues")
    @ResponseBody
    public JSONObject findByValues(String startTime, String endTime, Integer page, Integer limit) {
        JSONObject jo = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String stationCode = null;
            List<Patrol> allByValue = patrolService.findAllByValue(startTime, endTime, stationCode);
            List<Patrol> patrols = PageUtil.listPaging(allByValue, page, limit);
            JSONArray jsonArray = new JSONArray();
            for (Patrol patrol : patrols) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", patrol.getId());
//                jsonObject.put("stationCode",patrol.getStationCode());
                jsonObject.put("name", patrol.getStationName());
                jsonObject.put("lat", patrol.getLat());
                jsonObject.put("log", patrol.getLng());
                jsonObject.put("remark", patrol.getPatrolMsg());
                jsonObject.put("man", patrol.getPatrolerName());
//                jsonObject.put("patrolerId",patrol.getPatrolerId());
                jsonObject.put("time1", sdf.format(patrol.getMonitorTime()));
                jsonObject.put("time2", sdf.format(patrol.getSubmitTime()));
                jsonArray.add(jsonObject);
            }
            jo.put("data", jsonArray);
            jo.put("total", allByValue == null ? 0 : allByValue.size());
            jo.put("code", 0);
            jo.put("msg", "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("data", null);
            jo.put("total", 10);
            jo.put("code", 0);
            jo.put("msg", "");
        }
        return jo;
    }


    @RequestMapping("findById")
    @ResponseBody
    public JSONObject findById(@RequestParam("id") String id) {
        JSONObject jo = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Patrol patrol = patrolMapper.findAllById(id);
            PatrolValue patrolValue = patrolValueMapper.findAllByValue(id);
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", patrol.getId());
            jsonObject.put("stationCode", patrol.getStationCode());
            jsonObject.put("stationName", patrol.getStationName());
            jsonObject.put("lat", patrol.getLat());
            jsonObject.put("lng", patrol.getLng());
            jsonObject.put("patrolMsg", patrol.getPatrolMsg());
            jsonObject.put("patrolerName", patrol.getPatrolerName());
            jsonObject.put("patrolerId", patrol.getPatrolerId());
            jsonObject.put("monitorTime", sdf.format(patrol.getMonitorTime()));
            jsonObject.put("submitTime", sdf.format(patrol.getSubmitTime()));
            jsonObject.put("NI", patrolValue.getPatrol1());
            jsonObject.put("NO", patrolValue.getPatrol2());
            jsonObject.put("EI", patrolValue.getPatrol3());
            jsonObject.put("EO", patrolValue.getPatrol4());
            jsonObject.put("HI", patrolValue.getPatrol5());
            jsonObject.put("HO", patrolValue.getPatrol6());
            jsonObject.put("DN", patrolValue.getPatrol7());
            jsonObject.put("DE", patrolValue.getPatrol8());
            jsonObject.put("DH", patrolValue.getPatrol9());
            jsonObject.put("DF", patrolValue.getPatrol10());
            jsonObject.put("DG", patrolValue.getPatrol11());
            jsonArray.add(jsonObject);
            jo.put("data", jsonArray);
            jo.put("total", jsonArray.size());
            jo.put("code", 0);
            jo.put("msg", "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("data", null);
            jo.put("total", 10);
            jo.put("code", 0);
            jo.put("msg", "");
        }
        return jo;
    }

    @RequestMapping("delPatrl")
    @ResponseBody
    public JSONObject findByValues(String id) {
        JSONObject jo = new JSONObject();
        try {
            //删除巡检表
            patrolMapper.delById(id);
            //删除巡检值表
            patrolValueMapper.delByPatrolId(id);
            jo.put("state", 0);
            jo.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("state", 1);
            jo.put("msg", "删除失败");
        }
        return jo;
    }

    @RequestMapping("addPatrol")
    @ResponseBody
    public JSONObject addPatrol(Patrol patrol) {
        JSONObject jo = new JSONObject();
        try {
            patrolService.savePatrol(patrol);
            jo.put("state", 0);
            jo.put("msg", "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("state", 1);
            jo.put("msg", "保存失败");
        }
        return jo;
    }

    @RequestMapping("savePatrol")
    @ResponseBody
    public JSONObject savePatrol(Patrol patrol) {
        JSONObject jo = new JSONObject();
        try {
            patrolService.savePatrol(patrol);
            jo.put("state", 0);
            jo.put("msg", "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.put("state", 1);
            jo.put("msg", "保存失败");
        }
        return jo;
    }
}
